package com.CST.controller.cb;


import com.CST.common.api.ResultObject;
import com.CST.dto.cb.DateDTO;
import com.CST.dto.cb.InSourceDTO;
import com.CST.dto.ys.DataDTO;
import com.CST.entity.cb.CusUnitArea;
import com.CST.mapper.cst.cb.EtlInpatientIncomeMapper;
import com.CST.service.cb.IEtlInpatientIncomeService;
import com.CST.util.PageHelperTool;
import com.CST.vo.cb.InComeInSourceVO;
import com.CST.vo.cb.InSourceVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;

import java.math.BigDecimal;
import java.util.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

/**
* @author ly
* @since 2022-08-29
*/
@Api(tags = {""})
@RestController
@RequestMapping("/etlInpatientIncome2021")

    public class EtlInpatientIncomeController {

    @Autowired
    private IEtlInpatientIncomeService targetService;
    @Autowired
    private EtlInpatientIncomeMapper  etlInpatientIncomeMapper;

    @PostMapping("/HospitalizationIncome")
    @ApiOperation("获取住院收入明细")
    public List<InSourceVO> getHospitalizationList(@RequestBody InSourceDTO entity) {
        return targetService.getHospitalizationList(entity);

    }

    @PostMapping("/getInComeListDetail")
    @ApiOperation("获取收入明细")
    public ResultObject<PageInfo<InComeInSourceVO>>  getInComeListDetail(@RequestBody DateDTO dateDTO) {
        List<InComeInSourceVO> inComeListDetail = targetService.getInComeListDetail(dateDTO);
        BigDecimal totalAmount = inComeListDetail.stream()
                .map(item -> new BigDecimal(item.getAmount()))
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        PageInfo<InComeInSourceVO> cusUnitAreaPageInfo = PageHelperTool.excutePageInfo(inComeListDetail, dateDTO.getPageNum(), dateDTO.getPageSize());
        List<InComeInSourceVO> pageList = cusUnitAreaPageInfo.getList();
        if (pageList != null && pageList.size() > 0) {
            for (InComeInSourceVO inComeInSourceVO : pageList) {
                inComeInSourceVO.setTotalAmount(totalAmount.toString());
            }
        }
        cusUnitAreaPageInfo.setList(pageList);

        return ResultObject.success(cusUnitAreaPageInfo);

    }


    @PostMapping("/getInComeListDetailByPatientNo")
    @ApiOperation("获取收入明细")
    public ResultObject<PageInfo<InComeInSourceVO>>  getInComeListDetailByPatientNo(@RequestBody DateDTO dateDTO) {
        List<InComeInSourceVO> inComeListDetail = etlInpatientIncomeMapper.getInComeListDetailByPatientNo(dateDTO);
        BigDecimal totalAmount = inComeListDetail.stream()
                .map(item -> new BigDecimal(item.getAmount()))
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        PageInfo<InComeInSourceVO> cusUnitAreaPageInfo = PageHelperTool.excutePageInfo(inComeListDetail, dateDTO.getPageNum(), dateDTO.getPageSize());
        List<InComeInSourceVO> pageList = cusUnitAreaPageInfo.getList();
        if (pageList != null && pageList.size() > 0) {
            for (InComeInSourceVO inComeInSourceVO : pageList) {
                inComeInSourceVO.setTotalAmount(totalAmount.toString());
            }
        }
        cusUnitAreaPageInfo.setList(pageList);

        return ResultObject.success(cusUnitAreaPageInfo);

    }

}

